require('./PageSoftware.styl');

const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
import { Storage } from 'clientConfig/util/StoreData';
const { Group, TextField, SelectField, PhotoField, Icon, Button, DatetimeField, Toast } = SaltUI;
const Fileselect = require('components/fileselect')
import Addpersonfield from 'components/addpersonfield'

class Software extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            upfileName: [],
            approvers: [],//审批人列表
            proName: '' || props.pagedata && props.pagedata.proj_name,
            proNum: '' || props.pagedata && props.pagedata.proj_num,
            robot: '' || props.pagedata && props.pagedata.proj_webhook
        };
        // this.data = [
        //     { name: '合同签订时间', startTime: 'proTime' },
        //     { name: '方案评审', startTime: 'reviewStartTime', endTime: 'reviewEndTime' },
        //     { name: '项目实施／开发', startTime: 'developStartTime', endTime: 'developEndTime' },
        //     { name: '内部测试', startTime: 'reportStartTime', endTime: 'reportEndTime' },
        //     { name: '第三方测试', startTime: 'testStartTime', endTime: 'testEndTime' },
        //     { name: '项目初验', startTime: 'firstStartTime', endTime: 'firstEndTime' },
        //     { name: '试运行', startTime: 'runStartTime', endTime: 'runEndTime' },
        //     { name: '项目终验', startTime: 'finalStartTime', endTime: 'finalEndTime' },
        // ];
        this.data = [
            { name: '合同签订时间', startTime: 'proStartTime' },
            { name: '方案评审', startTime: 'reviewStartTime' },
            { name: '项目实施／开发', startTime: 'developStartTime', endTime: 'developEndTime' },
            { name: '内部测试', startTime: 'reportStartTime', endTime: 'reportEndTime' },
            { name: '第三方测试', startTime: 'testStartTime', endTime: 'testEndTime' },
            { name: '项目初验', startTime: 'firstStartTime' },
            { name: '试运行', startTime: 'runStartTime', endTime: 'runEndTime' },
            { name: '项目终验', startTime: 'finalStartTime' },
        ];
        if (props.pagedata) {
            let nodelists = props.pagedata.form_component_values.nodelists;
            for (let i = 0; i < this.data.length; i++) {

                if (this.data[i].startTime) {
                    this.state[this.data[i].startTime] = new Date(nodelists[i].node_info[0].value).getTime();
                }
                if (this.data[i].endTime) {
                    this.state[this.data[i].endTime] = new Date(nodelists[i].node_info[1].value).getTime();
                }
            }
            this.state.edit = props.edit;
            this.state.approvers = props.pagedata.approvers;
            this.state.upfileName = JSON.parse(props.pagedata.form_component_values.content.attach_info);
        }
    }
    handleTextChange(name, newValue) {
        this.setState({
            [name]: newValue
        });
    }
    //时间选择器选择
    handleChange(name, value) {
        const fn = (item) => {
            return item.startTime == name || item.endTime == name;
        }
        let num = this.data.findIndex(fn);
        for (let j = 0; j < num; j++) {
            if (!this.state[this.data[j].startTime]) {
                Toast.show({
                    type: 'error',
                    content: `${this.data[j].name}开始时间不能为空`
                });
                return;
            }
            if (this.data[j].hasOwnProperty('endTime')) {
                if (!this.state[this.data[j].endTime]) {
                    Toast.show({
                        type: 'error',
                        content: `${this.data[j].name}结束时间不能为空`
                    });
                    return;
                }
            }
        }
        //时间大小判断
        if (name.includes("StartTime")) {
            // debugger
            //和下一节点比较
            if (this.data[num].hasOwnProperty('endTime')) {
                if (this.state[this.data[num].endTime] && value > this.state[this.data[num].endTime]) {
                    Toast.show({
                        type: 'error',
                        content: `当前时间必须不大于节点结束时间`
                    });
                    return;
                }
            } else {
                // debugger
                if (num <= this.data.length - 2 && this.state[this.data[num + 1].startTime] && value >= this.state[this.data[num + 1].startTime]) {
                    Toast.show({
                        type: 'error',
                        content: `当前时间必须不大于节点结束时间`
                    });
                    return;
                }
            }
            //和上一节点比较
            if (num > 0) {
                if (this.data[num - 1].hasOwnProperty('endTime')) {
                    if (value <= this.state[this.data[num - 1].endTime]) {
                        Toast.show({
                            type: 'error',
                            content: `当前时间必须大于上一节点结束时间`
                        });
                        return;
                    }
                } else {
                    if (value <= this.state[this.data[num - 1].startTime]) {
                        Toast.show({
                            type: 'error',
                            content: `当前时间必须大于上一节点时间`
                        });
                        return;
                    }
                }
            }
        } else if (name.includes("EndTime")) {
            if (!this.state[this.data[num].startTime]) {
                Toast.show({
                    type: 'error',
                    content: `${this.data[num].name}开始时间未设置`
                });
                return;
            }
            if (value < this.state[this.data[num].startTime]) {
                Toast.show({
                    type: 'error',
                    content: `结束时间必须大于等于开始时间`
                });
                return;
            }
            if (num <= this.data.length - 2 && this.state[this.data[num + 1].startTime] && value >= this.state[this.data[num + 1].startTime]) {
                Toast.show({
                    type: 'error',
                    content: `当前时间必须小于下一节点时间`
                });
                return;
            }
            // if (num == 0 && this.state[this.data[num + 1].startTime] && value >= this.state[this.data[num + 1].startTime]) {
            //     Toast.show({
            //         type: 'error',
            //         content: `当前时间必须小于下一节点时间`
            //     });
            //     return;
            // }
        }
        this.setState({
            [name + '1']: new Date(value).toLocaleDateString(),
            [name]: value
        });
    }
    //提交按钮
    handleClick() {
        if (!this.state['proName']) {
            Toast.show({
                type: 'error',
                content: `项目名称不能为空`
            });
            return;
        } else if (!this.state['proNum']) {
            Toast.show({
                type: 'error',
                content: `项目编号不能为空`
            });
            return;
        } else if (!this.state['proStartTime']) {
            Toast.show({
                type: 'error',
                content: `合同签订时间不能为空`
            });
            return;
        }
        for (let i = 0; i < this.data.length; i++) {
            if (this.data[i].hasOwnProperty('endTime')) {
                if (!this.state[this.data[i].startTime] && !this.state[this.data[i].endTime]) {
                    Toast.show({
                        type: 'error',
                        content: `${this.data[i].name}时间不能为空`
                    });
                    return;
                }
            }
            if (!this.state[this.data[i].startTime]) {
                Toast.show({
                    type: 'error',
                    content: `${this.data[i].name}开始时间不能为空`
                });
                return;
            } else if (this.data[i].hasOwnProperty('endTime') && !this.state[this.data[i].endTime]) {
                Toast.show({
                    type: 'error',
                    content: `${this.data[i].name}结束时间不能为空`
                });
                return;
            }
        }
        if (!this.state['robot']) {
            Toast.show({
                type: 'error',
                content: `群地址不能为空`
            });
            return;
        } else if (!this.refs.fileselect.fileData || this.refs.fileselect.fileData.length == 0) {
            Toast.show({
                type: 'error',
                content: `附件不能为空`
            });
            return;
        } else if (this.state.approvers.length == 0) {
            Toast.show({
                type: 'error',
                content: `审批人不能为空`
            });
            return;
        }
        if (this.props.pagedata) {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, edit: this.state.edit, pagedata: this.props.pagedata });
        } else {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData });
        }

    }
    handleAddpersonCb(names) {
        this.setState({ approvers: names })
    }
    handleAddpersonUnionid(ids) {
        this.setState({
            approvers_id: ids
        })
    }

    render() {
        let t = this;
        return (
            <div className="software myform">
                <Group.List >
                    <TextField
                        label="项目名称"
                        placeholder="请输入项目名称"
                        value={t.state.proName}
                        onChange={t.handleTextChange.bind(t, 'proName')}
                        readOnly={t.props.pagedata ? true : t.props.edit}
                    />
                </Group.List>
                <Group.List >
                    <TextField
                        label="项目编号"
                        placeholder="请输入项目编号"
                        value={t.state.proNum}
                        onChange={t.handleTextChange.bind(t, 'proNum')}
                        readOnly={t.props.pagedata ? true : t.props.edit}
                    />
                </Group.List>
                {this.data.map((item, index) => {
                    if (!item.hasOwnProperty("endTime")) {
                        return (<Group.List >
                            <DatetimeField
                                label={`${item.name}`}
                                placeholder="请选择"
                                columns={['年', '月', '日']}
                                className="time"
                                value={this.state[item.startTime]}
                                readOnly={t.props.edit}
                                onSelect={this.handleChange.bind(t, item.startTime)} />
                        </Group.List>)
                    } else {
                        return <div>
                            <div className="timeTitle">{item.name}</div>
                            <Group.List>
                                <DatetimeField
                                    label="开始时间"
                                    readOnly={t.props.edit}
                                    placeholder="请选择"
                                    columns={['年', '月', '日']}
                                    className="time"
                                    value={this.state[item.startTime]}
                                    onSelect={this.handleChange.bind(t, item.startTime)} />
                                <DatetimeField
                                    label="结束时间"
                                    placeholder="请选择"
                                    columns={['年', '月', '日']}
                                    className="time"
                                    readOnly={t.props.edit}
                                    value={this.state[item.endTime]}
                                    onSelect={this.handleChange.bind(t, item.endTime)} />
                            </Group.List>
                        </div>
                    }
                })}
                <Group.List >
                    <TextField
                        label="群地址"
                        placeholder="请输入群地址"
                        value={t.state.robot}
                        onChange={t.handleTextChange.bind(t, 'robot')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Fileselect
                    ref='fileselect'
                    fold={true}
                    upfileName={this.state.upfileName}
                    proName={{ text: this.state.proName }}
                    edit={this.props.edit}
                    folder_id={0}
                    pathdata=''
                    path_name=''
                    message="workplan-software"
                />
                <Addpersonfield
                    label="发送给：监理 -- 甲方"
                    cb={t.handleAddpersonCb.bind(t)}
                    union={t.handleAddpersonUnionid.bind(t)}
                    maxLength="3"
                    readOnly={t.props.edit}
                    approvers={t.state.approvers}
                    approvers_id={t.state.approvers_id}
                />
                <Button type="primary" className="submitBtn" onClick={t.handleClick.bind(t)}>提交</Button>
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
    }

    componentWillReceiveProps(nextProps) {
        const t = this,
            { edit } = t.state;
        if (edit != nextProps.edit)
            this.setState({ edit: nextProps.edit })
        if (nextProps.pagedata.approvers && nextProps.pagedata.approvers instanceof Array)
            this.setState({
                approvers: nextProps.pagedata.approvers,
                approvers_id: nextProps.pagedata.approvers_id,
            });
        else if (nextProps.pagedata.approvers && typeof nextProps.pagedata.approvers == 'string')
            this.setState({
                approvers: [nextProps.pagedata.approvers],
                approvers_id: [nextProps.pagedata.approvers_id]
            });
        if (nextProps.pagedata.cc_list && nextProps.pagedata.cc_list instanceof Array)
            this.setState({
                cc_list: nextProps.pagedata.cc_list,
                cc_list_id: nextProps.pagedata.cc_list_id
            });
        else if (nextProps.pagedata.cc_list && typeof nextProps.pagedata.cc_list == 'string')
            this.setState({
                cc_list: [nextProps.pagedata.cc_list],
                cc_list_id: [nextProps.pagedata.cc_list_id]
            })
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {


    }

}

reactMixin.onClass(Software, Reflux.connect(Store));

module.exports = Software;
